package com.example.test_app;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.sortndk.OhuangUtil;
import com.example.test_app.Util.ClassCopier;
import com.example.test_app.Util.HTTPSUtils;
import com.example.test_app.Util.LogActivity;
import com.example.test_app.Util.PhotoUtil;
import com.example.test_app.databinding.ActivityMainBinding;
import com.example.test_app.share.FileUtil;
import com.example.test_app.share.Share2;
import com.example.test_app.share.ShareContentType;
import com.example.test_app.livedata.LiveDataBus;
import com.example.test_app.view.GlideImageGeter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Set;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends LogActivity {
    private static final String DEFAULT_KEY_NAME = "aaaa";
    ActivityMainBinding binding;
    int[] a = {5, 10, 1, 0, -5, 6, 5, 7, 3, 5};
    Set<String> set;

    private OkHttpClient okHttpClient;

    public static final String TAG = MainActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        getSupportFragmentManager().beginTransaction().add(new BlankFragment2(), "1")
                .commit();

        LiveDataBus.get().with("1", String.class).setData("刷新成功");


        // OhuangUtil.sort(a);
        binding.text.setText(Arrays.toString(a));
        binding.text.setOnClickListener(v -> {
            Activity activity=new Activity(){
                @Override
                public void startActivity(Intent intent) {
                    super.startActivity(intent);
                    Log.d(TAG, "startActivity: ");
                }
            };
            ClassCopier classCopier = new ClassCopier(Activity.class);
            classCopier.copy(MainActivity.this,activity);
            Intent intent = new Intent(this, RvActivity.class);
            int flag =  Intent.FLAG_ACTIVITY_NEW_TASK;
            intent.setFlags(flag);
            activity.startActivity(intent);

        });
        binding.text2.setOnClickListener(v -> {
                    PhotoUtil.INSTANCE.getPhotoLib(this, true);
                }
        );

        binding.text2.setFocusableInTouchMode(true);
        binding.text2.requestFocus();
        binding.text2.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                Log.d("binding.text2", "onKey: ");
                return false;
            }
        });

        binding.layoutMain2.setOnClickListener(v -> {

            Request request = new Request.Builder()
                    .get().url("https://www.baidu.com/")
                    .build();
            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(@NonNull Call call, @NonNull IOException e) {
                    Log.d("MainActivity", "onFailure: " + e.toString());
                }

                @Override
                public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                    Log.d("MainActivity", "onResponse: " + response.body().string());

                }
            });

        });
        binding.ivMain.setOnClickListener(v -> {
            sharde();

        });

        StringBuilder buffer = new StringBuilder();
        buffer.append(" <font color='#00FFDC'>").append("你好啊").append("</font>");
        buffer.append("<br> <img src='https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F017c3f56f8f1d432f875a94468278c.gif&refer=http%3A%2F%2Fimg.zcool.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1637225185&t=582e1f819b9aa55b9280a73a6f58d1b9'></img>");
        String strHtml1 = buffer.toString();

        GlideImageGeter glideImageGeter = new GlideImageGeter(this, binding.text, 200);
        Spanned spanned = Html.fromHtml(strHtml1, glideImageGeter, null);
        SpannableStringBuilder spannableString = new SpannableStringBuilder(spanned);
        binding.text.setText(spanned);
        initOK();


    }

    private void sharde() {
        Log.d("TAG", "sharde:file= " + file.getPath());
        Uri uri = FileUtil.getFileUri(this, ShareContentType.FILE, file);
        Log.d("TAG", "sharde: uri=" + uri.toString());
        new Share2.Builder(this)
                .setContentType(ShareContentType.FILE)
                .setShareFileUri(uri)
                .setTitle("Share Image")
                .build()
                .shareBySystem();
    }

    private void initOK() {
        OkHttpClient.Builder builder = new OkHttpClient.
                Builder();
//        okHttpClient = builder.
//                addInterceptor(new ChuckInterceptor(getApplicationContext()))
//                .sslSocketFactory(SSLSocketClient.getSSLSocketFactory(), new X509TrustManager() {
//                    @Override
//                    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//
//                    }
//
//                    @Override
//                    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
//
//                    }
//
//                    @Override
//                    public X509Certificate[] getAcceptedIssuers() {
//                        return new X509Certificate[0];
//                    }
//                })//配置
//                .hostnameVerifier(SSLSocketClient.getHostnameVerifier())//配置
//                .build();
        okHttpClient = new HTTPSUtils(this, "key/hj.cer").getInstance();
    }


    private void write() {

    }

    File file;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        file = PhotoUtil.INSTANCE.onActivityResult(requestCode
                , resultCode, data, this, "");
        if (file == null) {
            return;
        }
        try {

            Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
            binding.ivMain.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("MainActivity", "onDestroy: ");
    }
}